<html>
    <head>
        <script src="../../../LayoutTests/media/video-test.js"></script>
        <script>

            var seekedCount = 0;
            var counter = 0;

            function trySeek(seekTo)
            {
                if (isInTimeRanges(video.seekable, seekTo)) {
                    if (isInTimeRanges(video.buffered, seekTo)) {
                        consoleWrite("Warining: Seeking into buffered region. May not generate waiting or seeking events.");
                    }
                    run("video.currentTime = " + seekTo);
                } else {
                    failTest("Cannot seek to " + seekTo + ". Does engine support seeking into unbuffered region?");
                }
            }

            function seeked()
            { 
                ++seekedCount; 
                consoleWrite("");
            }

            function scheduleSeek() {
                setTimeout(someTimeLater, 200);
            }

            function someTimeLater()
            {
                consoleWrite("Running scheduled seek");
                testExpected("seekedCount", counter);
                testExpected("video.currentTime", counter, ">=");

                if (counter == 3)
                {
                    consoleWrite("");
                    return;
                    endTest();
                }

                ++counter;

                scheduleSeek();
                trySeek(counter);
                consoleWrite("");
            }

            function start()
            {
                findMediaElement();

                waitForEvent('waiting');
                waitForEvent('seeking');
                waitForEvent('seeked', seeked);
                waitForEvent('play', someTimeLater);

                consoleWrite("Start Load");
                video.src = "http://127.0.0.1:8000/media/video-throttled-load.cgi?name=../../../media/content/test.mp4&throttle=25";
                video.load();
                video.play();
            }
        </script>
    </head>
    <body onload="start()">
        <video controls></video>

        <p>Attempt to seek out of the buffered range.  If supported, this
        should cause a waiting and seeking event to be fired for each seek.
        This test makes assumptions on the size of the input file, the bitrate
        of delivery, and the duration of the file.
        </p>
        <p>
        Source is assumed to be about 188k with a uniform bitrate, &gt; 4
        seconds in length, and served at 25kb/s.  This should make seeking one
        second ahead every 200ms always leave the buffered region.
        </p>
    </body>
</html>
